<?php
require_once('emails.php');
require_once('db_login.php');
require_once('common.php');


$sql  = "SELECT players.email, participations.codeword, participations.alias ";
	$sql .= "FROM players INNER JOIN participations ON players.playerId = participations.playerId ";
	$sql .= "WHERE players.playerId=11;";
	$result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]);
	while($row = mysql_fetch_assoc($result)) {
		sendGamePending($row['email'], "aaa", $row['alias']);
	}
function getPlayerStats($playerId) {
		$playerStats = array();
		
		$sql2 ="SELECT gameAssassinations.playerId, gameAssassinations.name, MAX(killsPerGame) as killingSpree FROM
(SELECT players.playerId, players.name, gamesInquery.gameId, COUNT(gamesInquery.gameId) as killsPerGame
FROM players LEFT JOIN (SELECT * FROM assassinations WHERE assassinations.state = 'SUCCESS') as assassinInquery on assassinInquery.assassinId = players.playerId 
LEFT JOIN (SELECT * FROM games WHERE games.state = 'FINISHED') as gamesInquery ON assassinInquery.gameId = gamesInquery.gameId 
GROUP BY players.playerId, players.name, gamesInquery.gameId) as gameAssassinations WHERE gameAssassinations.playerId =  '$playerId' 
GROUP BY gameAssassinations.playerId, gameAssassinations.name" ;

		$sql  = "SELECT players.playerId, players.name, ";
		$sql .= "(SELECT COUNT(*) FROM assassinations INNER JOIN games ON assassinations.gameId = games.gameId WHERE assassinations.assassinId = players.playerId AND assassinations.state = 'SUCCESS' AND games.state = 'FINISHED') AS assassinationsCompleted, ";
		$sql .= "(SELECT COUNT(*) FROM assassinations WHERE assassinations.victimId = players.playerId AND (assassinations.state = 'SUCCESS' or assassinations.state = 'WANTED' or assassinations.state = 'SELF_DEFENSE') ) AS assassinated, ";
		$sql .= "(SELECT COUNT(*) FROM participations WHERE participations.playerId = players.playerId AND state != 'ACTIVE') AS gamesPlayed, ";
		$sql .= "(SELECT COUNT(*) FROM participations WHERE participations.playerId = players.playerId AND state = 'WON') AS gamesWon, ";
		$sql .= "(SELECT COUNT(likeId) FROM assassinations INNER JOIN likes ON assassinations.assassinationId = likes.assassinationId INNER JOIN games ON assassinations.gameId = games.gameId  WHERE assassinations.assassinId = 16 AND games.state = 'FINISHED' ) as  assassinationsLiked ";
		$sql .= "FROM players " . "WHERE players.playerId = '$playerId' " . "LIMIT 1000;";
		$result = mysql_query($sql) or sql_error_report($sql, $_SERVER["SCRIPT_NAME"]);

		while($row = mysql_fetch_assoc($result)) {
		
			$result2 = mysql_query($sql2) or sql_error_report($sql2, $_SERVER["SCRIPT_NAME"]);
			
			while($row2 = mysql_fetch_assoc($result2)) {
			
				$playerStat = array(
					"playerId" => $row['playerId'],
					"name" => $row['name'],
					"assassinations" => $row['assassinationsCompleted'],
					"assassinated" => $row['assassinated'],
					"gamesPlayed" => $row['gamesPlayed'],
					"gamesWon" => $row['gamesWon'],
					"killingSpree" => $row2['killingSpree'],
					"likes" => '0' //$row2['assassinationsLiked']
				);
			
				$ret = array(
					"status" => "OK",
					"playerStats" => $playerStat
				);
				print json_encode($ret);
				return;
			}
		}
		
		$ret = array(
			"status" => "NO_PLAYER"
		);
		print json_encode($ret);
	}
	
?>